<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\tinyinfo\tags;

use think\Exception;
use think\template\TagLib;

class TagTinyinfo extends TagLib {

    protected $tags   =  [
        'get_one'      => ['attr' => 'value,tinyinfo_code,index','close' => 0],
        'get_list'      => ['attr' => 'value,where,order,page,limit','close' => 0],
        'get_code_list'      => ['attr' => 'value','close' => 0],
    ];

    public function tagGet_One($tag){

        $value = $tag['value'];

        $tinyinfo_code = !empty($tag['tinyinfo_code'])?$tag['tinyinfo_code']:'';
        if(!preg_match('/[a-zA-Z]{1}[a-zA-Z0-9]*/',$tinyinfo_code)){
            throw new Exception('tinyinfo_code必须为英文字母和阿拉伯数字，第一个字母必须为英文字母。');
            return;
        }
        $tinyinfo_code = $this->convertParamForTpl($tinyinfo_code);

        $index = !empty($tag['index'])?$tag['index']:1;

        $modelName = 'app\\\\tinyinfo\model\Tinyinfo';

        $html='<?php  $'.$value.' = model("'.$modelName.'")->getOne( '.$tinyinfo_code.','.$index.');?>';
        return $html;
    }

    public function tagGet_List($tag){

        $value = $tag['value'] ;

        $where = !empty($tag['where'])?$tag['where']:"null"  ;

        $order = !empty($tag['order'])?$tag['order']:"'id desc'";

        $page = !empty($tag['page'])?$tag['page']:1;

        $limit = !empty($tag['limit'])?$tag['limit']:100;

        $modelName = 'app\\\\tinyinfo\model\Tinyinfo';
        $html='<?php  $'.$value.' = model("'.$modelName.'")->getList( '.$where.','.$order.','.$page.','.$limit.');?>';
        return $html;
    }

    public function tagGet_Code_List($tag){
        $value = $tag['value'];

        $html='<?php  $'.$value.' = db("tinyinfoCode")->select(); ?>';
        return $html;
    }



}
